<?php
class pro_lap_permintaan_model extends MY_Model {
	var $model = 'pro_lap_permintaan';

	// function getApp() {
	// 	$ls_sql	= "	SELECT	NAMA_BARANG value,
	// 						NAMA_BARANG display
	// 				FROM	ADMIN_PRIMKOKAS.PRO_SETUP_STOCK
	// 				ORDER BY NAMA_BARANG";
					
	// 	$result	= $this->db->query($ls_sql);
	// 	if($result->num_rows() > 0){
	// 		return $result->result_array();
	// 	}else{
	// 		return array();
	// 	}
	// }
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT
					a.ID_PERMINTAAN,
					a.TANGGAL,
					a.NAMA,
					b.NAMA_DEPARTEMEN as DEPARTEMEN,
					a.PERMOHONAN_MASALAH,
					concat('- ',REPLACE(a.PILIHAN_PERMINTAAN, ',', '<br>- ')) as PILIHAN_PERMINTAAN
					FROM	PRO_DATA_PERMINTAAN_PERANGKAT a
					left join HRD_DEPARTEMEN b
					on a.DEPARTEMEN = b.ID_DEPARTEMEN_PK
					WHERE	(UPPER(a.ID_PERMINTAAN) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.TANGGAL) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.NAMA) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.NAMA_DEPARTEMEN) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.PERMOHONAN_MASALAH) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.PILIHAN_PERMINTAAN) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.ID_PERMINTAAN' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON_detail($id)
	{
		$ls_sql	= "	SELECT
							ROWNUM as no,
							a.ID_PERMINTAAN_DTL,
							a.ID_PERMINTAAN,
							a.ID_BARANG,
							b.NAMA_BARANG,
							a.JUMLAH,
							a.LOKASI,
							a.KETERANGAN
							FROM
							PRO_DATA_PERMINTAAN_PRK_DTL a
							left join PRO_SETUP_STOCK b
							on a.ID_BARANG = b.KODE_BARANG
							WHERE a.ID_PERMINTAAN = $id
							and	(UPPER(b.NAMA_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.LOKASI) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.KETERANGAN) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.ID_PERMINTAAN' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('pro_barang', $arr_data);
		
		/*$this->AuditTrail(	'sample_master',// Table
							'Create',		// Action
							''				// Primary Key
							);*/
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('admin_primkokas.pro_barang', $arr_data, $arr_rule);
		
		/*$this->AuditTrail(	'sample_master',		// Table
							'Update',				// Action
							json_encode($arr_rule)	// Primary Key
							);*/
	}
	
	function GetSingle($as_pkmaster){
		$result = $this->db->get_where('sample_master', array('ref_key' => $as_pkmaster));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($ID_PERMINTAAN)
	{
		$result = $this->db->query("SELECT
					a.ID_PERMINTAAN,
					TO_CHAR(a.TANGGAL, 'DD/MM/YYYY') as TANGGAL, 
					a.NAMA,
					a.DEPARTEMEN as DEPARTEMEN,
					b.NAMA_DEPARTEMEN as NAMA_DEPARTEMEN,
					a.PERMOHONAN_MASALAH,
					a.PILIHAN_PERMINTAAN
					FROM	PRO_DATA_PERMINTAAN_PERANGKAT a
					left join HRD_DEPARTEMEN b
					on a.DEPARTEMEN = b.ID_DEPARTEMEN_PK
					WHERE ID_PERMINTAAN = '$ID_PERMINTAAN'");

		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function GetSingleView_detail($ID_PERMINTAAN_dtl)
	{
		$result = $this->db->query("SELECT
			ROWNUM as no,
			a.ID_PERMINTAAN_DTL,
			a.ID_PERMINTAAN,
			a.ID_BARANG as id_barangan,
			b.NAMA_BARANG,
			a.JUMLAH,
			a.LOKASI,
			a.KETERANGAN
			FROM
			PRO_DATA_PERMINTAAN_PRK_DTL a
			left join PRO_SETUP_STOCK b
			on a.ID_BARANG = b.KODE_BARANG
			WHERE a.ID_PERMINTAAN_DTL = '$ID_PERMINTAAN_dtl'");

		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {

			$this->db->delete('ADMIN_PRIMKOKAS.PRO_DATA_PERMINTAAN_PERANGKAT', $arr_rule);
			$this->db->delete('ADMIN_PRIMKOKAS.PRO_DATA_PERMINTAAN_PRK_DTL', $arr_rule);
			
			/*$this->AuditTrail(	'sample_master',		// Table
								'Delete',				// Action
								json_encode($arr_rule)	// Primary Key
								);*/
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function DeleteSingleDetail($arr_rule)
	{
		try {

			$this->db->delete('ADMIN_PRIMKOKAS.PRO_DATA_PERMINTAAN_PRK_DTL', $arr_rule);
			
			/*$this->AuditTrail(	'sample_master',		// Table
								'Delete',				// Action
								json_encode($arr_rule)	// Primary Key
								);*/
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GetCetak($ID_permintaan)
	{
		$result1 = $this->db->query(" 	SELECT
										a.ID_permintaan,
										a.TANGGAL,
										a.NAMA,
										b.NAMA_DEPARTEMEN as DEPARTEMEN,
										a.PERMOHONAN_MASALAH,
										a.PILIHAN_permintaan
										FROM	PRO_DATA_permintaan_PERANGKAT a
										left join HRD_DEPARTEMEN b
										on a.DEPARTEMEN = b.ID_DEPARTEMEN_PK
										WHERE ID_permintaan = '".$ID_permintaan."'
									");
		return $result1->row_array();
		
	}

	function GetCetakList($ID_permintaan)
	{
		$result2 = $this->db->query(" 	SELECT
										ROWNUM as no,
										a.ID_permintaan_DTL,
										a.ID_permintaan,
										a.ID_BARANG,
										b.NAMA_BARANG,
										a.JUMLAH,
										a.LOKASI,
										a.KETERANGAN
										FROM PRO_DATA_permintaan_PRK_DTL a
										left join PRO_SETUP_STOCK b
										on a.ID_BARANG = b.KODE_BARANG
										WHERE ID_permintaan = '".$ID_permintaan."'
										");
		return $result2->result_array();
		
	}
}
?>